<template>
  <div class="tree-node" :class="{ 'is-expanded': node.expanded }">
    <div class="tree-node-content" :style="{ paddingLeft: level * 18 + 'px' }" @click="handleClick"
      :class="{ 'is-selected': isSelected, 'is-parent-of-selected': isParentOfSelected }">
      <span v-if="hasChildren" class="tree-node-expand-icon" :class="{ 'is-expanded': node.expanded }">
        <img style="width:10px;" :src="getAssetsFile('img_header', 'user_down')" />
      </span>
      <span v-else class="tree-node-leaf-icon"></span>
      <span class="tree-node-label"
        :class="{ 'is-selected': isSelected, 'is-parent-of-selected': isParentOfSelected }">{{ node.label }}</span>
    </div>
    <div v-if="node.expanded && hasChildren" class="tree-node-children">
      <tree-node v-for="child in node.children" :key="child.id" :node="child" :level="level + 1"
        :selected-node="selectedNode" @node-click="handleChildNodeClick" />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { getAssetsFile } from '@/utils/compnt'

// 定义组件属性
const props = defineProps({
  node: {
    type: Object,
    required: true
  },
  level: {
    type: Number,
    default: 0
  },
  selectedNode: {
    type: Object,
    default: null
  }
})

// 定义组件事件
const emit = defineEmits(['node-click'])

// 计算属性：判断是否有子节点
const hasChildren = computed(() => {
  return props.node.children && props.node.children.length > 0
})

// 计算属性：判断当前节点是否被选中
const isSelected = computed(() => {
  return props.selectedNode && props.selectedNode.id === props.node.id
})

// 计算属性：判断当前节点是否是选中节点的父级
const isParentOfSelected = computed(() => {
  if (!props.selectedNode) return false

  // 递归检查当前节点的所有子节点是否包含选中的节点
  const checkChildren = (children) => {
    if (!children || children.length === 0) return false

    for (let child of children) {
      if (child.id === props.selectedNode.id) {
        return true
      }
      if (checkChildren(child.children)) {
        return true
      }
    }

    return false
  }

  return checkChildren(props.node.children)
})

/**
 * 处理节点点击事件
 * @param {Event} event - 点击事件对象
 */
const handleClick = (event) => {
  emit('node-click', props.node)
}

/**
 * 处理子节点点击事件
 * @param {Object} node - 被点击的子节点数据
 */
const handleChildNodeClick = (node) => {
  emit('node-click', node)
}
</script>

<style lang="less" scoped>
.tree-node {
  user-select: none;
  margin: 5px 0;

  &-content {
    display: flex;
    align-items: center;
    padding: 8px 0;
    cursor: pointer;
    transition: background-color 0.2s;

    &:hover {
      background: rgba(235, 243, 255, 0.1);
    }

    &.is-selected {
      background: rgba(235, 243, 255, 0.1);
    }

    &.is-parent-of-selected {
      background: rgba(235, 243, 255, 0.1);
    }
  }

  &-expand-icon {
    width: 16px;
    height: 16px;
    font-size: 12px;
    text-align: center;
    margin-right: 8px;
    margin-left: 15px;
    color: #c0c4cc;
    transition: transform 0.2s;
    transform: rotate(-90deg);

    &.is-expanded {
      transform: rotate(0deg);
    }

    .is-selected {
      color: #3BDEFF;
    }
  }

  &-leaf-icon {
    width: 16px;
    height: 16px;
    margin-right: 6px;

   
  }

  &-label {
    font-size: 14px;
    color: #93B0EB;
    transition: color 0.2s;

    &.is-selected    {
      color: #3BDEFF;
    }
  }

  &-children {
    overflow: hidden;
  }
}
</style>